package com.ct.application.shops.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shops.convert.ShopsCategoryConvert;
import com.ct.application.shops.model.po.ShopsCategoryAddPO;
import com.ct.application.shops.model.po.ShopsCategoryQueryPO;
import com.ct.application.shops.model.po.ShopsCategoryUpdatePO;
import com.ct.application.shops.model.vo.ShopsCategoryVO;
import com.ct.repository.commons.PageUtils;
import com.ct.shops.orm.ShopsCategory;
import com.ct.shops.repository.admin.IShopsCategoryRepository;
import com.ct.utils.valid.ValidatorUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 门店分类表 服务实现类
 * </p>
 *
 * @author system
 */
@Service
public class ShopsCategoryService {

    @Resource
    private ShopsCategoryConvert shopsCategoryConvert;
    @Resource
    private IShopsCategoryRepository shopsCategoryRepository;

    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return shopsCategoryRepository.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return shopsCategoryRepository.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }
    /**
     * 获取门店分类列表数据
     */
    public PageUtils<ShopsCategoryVO> getShopsCategoryList(ShopsCategoryQueryPO queryPO) {
        LambdaQueryWrapper<ShopsCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(queryPO.getKeyword()), ShopsCategory::getName, queryPO.getKeyword());
        lambdaQueryWrapper.eq(!Objects.isNull(queryPO.getStatus()), ShopsCategory::getStatus, queryPO.getStatus());
        lambdaQueryWrapper.orderByAsc(ShopsCategory::getSorted);
        Page<ShopsCategory> page = new Page<>(queryPO.getPageNo(), queryPO.getPageSize());
        IPage<ShopsCategory> categoryIPage = shopsCategoryRepository.page(page, lambdaQueryWrapper);

        List<ShopsCategory> categoryList = categoryIPage.getRecords();
        if (categoryList.isEmpty()) {
            return PageUtils.getEmptyInstance();
        }
        List<ShopsCategoryVO> categoryVOList = shopsCategoryConvert.toShopsCategoryVOList(categoryList);
        return new PageUtils<>(categoryIPage, categoryVOList);
    }


    /**
     * 获取所有门店分类
     */
    public List<ShopsCategoryVO> getAllShopsCategoryList() {
        LambdaQueryWrapper<ShopsCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq( ShopsCategory::getStatus, 1);
        lambdaQueryWrapper.orderByAsc(ShopsCategory::getSorted);
        List<ShopsCategory> categoryList = shopsCategoryRepository.list(lambdaQueryWrapper);
        List<ShopsCategoryVO> categoryVOList = new ArrayList<>();
        if (categoryList.isEmpty()) {
            return categoryVOList;
        }
        return shopsCategoryConvert.toShopsCategoryVOList(categoryList);
    }


    /**
     * 根据id查询分类
     */
    public ShopsCategoryVO getShopsCategoryById(Long id) {
        ShopsCategory shopsCategory = shopsCategoryRepository.getById(id);
        if (shopsCategory == null) {
            return null;
        }
        ShopsCategoryVO shopsCategoryVO = new ShopsCategoryVO();
        BeanUtils.copyProperties(shopsCategory, shopsCategoryVO);
        return shopsCategoryVO;
    }


    /**
     * 新增门店分类
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(ShopsCategoryAddPO addPO) {
        ShopsCategory shopsCategory = new ShopsCategory();
        BeanUtils.copyProperties(addPO, shopsCategory);
        return shopsCategory.insert();
    }


    /**
     * 修改门店分类
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ShopsCategoryUpdatePO updatePO) {
        ShopsCategory shopsCategory = new ShopsCategory();
        BeanUtils.copyProperties(updatePO, shopsCategory);
        return shopsCategory.updateById();
    }


    /**
     * 删除分类
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return shopsCategoryRepository.removeById(id);
    }

}
